/****************************************************************************
  @file    : tvbox_84.c
  @date    : 2020-06-05
  @brief   : 
*****************************************************************************/
/* Includes ----------------------------------------------------------------*/
#include "remote.h"
#include "string.h"
#include "tvbox.h"

/* Private typedef ---------------------------------------------------------*/
/* Private define ----------------------------------------------------------*/
//ʱ�䶨��
#define  BOX_LEADCODE_TIME_H		(uint16_t)( 0x8000   | (uint16_t)9000 )
#define  BOX_LEADCODE_TIME_L		(uint16_t)( (~0x8000)& (uint16_t)4500 )
#define  BOX_ENDCODE_TIME_H			(uint16_t)( 0x8000   | (uint16_t)610  )
#define  BOX_ENDCODE_TIME_L	    	(uint16_t)( (~0x8000)& (uint16_t)20000)
#define  BOX_BIT0_TIME_H			(uint16_t)( 0x8000   | (uint16_t)610  )
#define  BOX_BIT0_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)540  )
#define  BOX_BIT1_TIME_H			(uint16_t)( 0x8000   | (uint16_t)610  )
#define  BOX_BIT1_TIME_L			(uint16_t)( (~0x8000)& (uint16_t)1640 )

//��ֵ����
static const uint8_t BOX_Code[TVBOX_KEY_NUM] = {
 0x00,	//0x01����չ-���أ�
 0x00,	//0x02����չ-AV/TV��
 0x00,	//0x03����չ-����+��
 0x00,	//0x04����չ-����-��
 0x00,	//0x05����չ-Ƶ��+��
 0x00,	//0x06����չ-Ƶ��-��
 0x00,	//0x07����չ-������
 0x00,	//0x08����չ-������
 0x00,	//0x09����չ-������
 0x00,	//0x0A����չ-������
 0xDC,	//0x0B�����أ�11
 0x82,	//0x0C���˵���
 0x00,	//0x0D����Ϣ��
 0xC5,	//0x0E�����أ�14
 0x9C,	//0x0F��������
 0x98,	//0x10��������
 0x80,	//0x11������+��17
 0x81,	//0x12������-��18
 0x85,	//0x13��Ƶ��+��19
 0x86,	//0x14��Ƶ��-��20
 0xCA,	//0x15���ϣ�
 0xD2,	//0x16���£�
 0x99,	//0x17����
 0xC1,	//0x18���ң�
 0xCE,	//0x19��ȷ����
 0x00,	//0x1A���ź�Դ��
 0x88,	//0x1B����ҳ��
 0x00,	//0x1C��������
 0x00,	//0x1D��������
 0x87,	//0x1E������0��30
 0x92,	//0x1F������1��
 0x93,	//0x20������2��
 0xCC,	//0x21������3��
 0x8E,	//0x22������4��
 0x8F,	//0x23������5��
 0xC8,	//0x24������6��
 0x8A,	//0x25������7��
 0x8B,	//0x26������8��
 0xC4,	//0x27������9��
 0xD0,	//0x28�����뷨��
};

/* Private macro -----------------------------------------------------------*/
/* Private variables -------------------------------------------------------*/
static uint16_t BoxSendlen = 0;

/* Private function prototypes ---------------------------------------------*/
static void PackageByte(uint8_t byte);

/* Private functions -------------------------------------------------------*/
static void PackageByte(uint8_t byte) {
	uint8_t i;

	for(i = 0; i<8; i++) {
		if(byte&(0x01<<(i))) {	//�ߵ�ƽ
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT1_TIME_L;
		} else {				//�͵�ƽ
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_H;
			IrSendBuf[BoxSendlen++] = BOX_BIT0_TIME_L;
		}
	}
}

/* Public functions --------------------------------------------------------*/
void TVBox84IrPackage(void *p) {
	uint8_t  i,p_byte = 0x00;

	uint8_t codeNum = *(uint8_t*)p;

	if(codeNum == 0 || codeNum >= TVBOX_KEY_NUM) return ;
	
	p_byte = BOX_Code[codeNum-1];

	if(p_byte == 0x00) return;
	
	//�ȴ��������
	//while(IrDev.State != IRREMOTE_IDLE);  
	memset(IrSendBuf, 0, sizeof(IrSendBuf)/sizeof(uint16_t));
	
	BoxSendlen = 0;

	//����������
	IrSendBuf[BoxSendlen++] = BOX_LEADCODE_TIME_H;
	IrSendBuf[BoxSendlen++] = BOX_LEADCODE_TIME_L;

	//���ÿ�����
	PackageByte(0xB3);
	PackageByte(0x4C);
	PackageByte(p_byte);
	PackageByte(~p_byte);

	//���ý�����
	IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_H;
	IrSendBuf[BoxSendlen++] = BOX_ENDCODE_TIME_L;
	
	//�������ύ
	IrDev.Ptr 		= IrSendBuf;
	IrDev.Len 		= BoxSendlen;
	IrDev.State 	= IRREMOTE_REQUIRE;
}
